package cn.jimu98.controller;

import cn.jimu98.entity.Post;
import cn.jimu98.vo.CommentVo;
import cn.jimu98.vo.PostVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

/**
 * @Classname PostController
 * @Description TODO
 * @Date 2020/6/7 15:10
 * @Created by kaibo
 */
@Controller
public class PostController extends BaseController{
    @GetMapping("/category/{id:\\d*}")
    public String category(@PathVariable(name = "id")Long id){
        int pn = ServletRequestUtils.getIntParameter(req,"pn",1);
        System.out.println("category");
        req.setAttribute("currentCategoryId", id);
        req.setAttribute("pn", pn);

        return "post/category";
    }
    @GetMapping("/post/{id:\\d*}")
    public String detail(@PathVariable(name = "id")Long id){
        PostVo vo = postService.selectOnePost(new QueryWrapper<Post>().eq("p.id",id));
        Assert.notNull(vo, "文章已被删除");

        postService.putViewCount(vo);

        //1.分页信息，2.分类，3.用户 4.置顶，5.精选 6.排序
        IPage<CommentVo> results=commentService.paing(getPage(),vo.getId(),null,"created");

        req.setAttribute("currentCategoryId", vo.getCategoryId());
        req.setAttribute("post", vo);
        req.setAttribute("pageData", results);
        System.out.println("detail");
        return "post/detail";
    }
}
